package cn.jiedh.app.meiweidk.Utils.UI;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Map;

import cn.jiedh.app.meiweidk.R;


public class XKPopupWindows extends PopupWindow {

	private ArrayList<String> list;
	private Map<String, String> lists;
	private Context context;
	private int width;
	private getSelectText getSelectText;
	private CloseView closeView;
	private MyBaseAdapter adapter;

	public XKPopupWindows(Context context, ArrayList<String> list , Map<String, String> lists , int width, CloseView closeView) {
		this.list = list;
		this.lists = lists;
		this.context = context;
		this.closeView = closeView;
		this.width =width;
		init();
	}
	
	private void init(){
		View view = LayoutInflater.from(context).inflate(R.layout.act_verifyreg_popupwindow,null);
		setContentView(view);
		setWidth(width);
		setHeight(LayoutParams.MATCH_PARENT);
		view.findViewById(R.id.pv).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
				closeView.getView();
			}
		});
		adapter = new MyBaseAdapter(context, list,lists ,R.layout.act_verifyreg_popupwindow_item);
		GridView listView = (GridView)view.findViewById(R.id.listview);
		listView.setAdapter(adapter);

	}
	
	class MyBaseAdapter extends BaseAdapter {
		Context context;
		ArrayList<String> data;
		private Map<String, String> listsa;
		int resource;

		public MyBaseAdapter(Context context, ArrayList<String> data, Map<String, String> lists , int resource) {
			this.context = context;
			this.data = data;
			this.listsa = lists;
			this.resource = resource;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View view = null;
			if(convertView == null){
				view = LayoutInflater.from(context).inflate(resource, null);
				((CheckBox)view.findViewById(R.id.medicalCardNum)).setText(data.get(position));


				((TextView)view.findViewById(R.id.medicalCardName)).setVisibility(View.GONE);
				view.findViewById(R.id.medicalCardNum).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						dismiss();
						getSelectText.getValue(list.get(position));	
					}
				});
				view.findViewById(R.id.medicalCardName).setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dismiss();
						getSelectText.getValue(list.get(position));
					}
				});

			} else {
				view = convertView;
			}
			if (listsa.get(""+position).equals("1")) {
				((CheckBox)view.findViewById(R.id.medicalCardNum)).setChecked(true);
//				((CheckBox)view.findViewById(R.id.medicalCardNum)).setBackgroundResource(R.drawable.btn_tran_orange);
			}else {
				((CheckBox)view.findViewById(R.id.medicalCardNum)).setChecked(false);
//				((CheckBox)view.findViewById(R.id.medicalCardNum)).setBackgroundResource(R.drawable.btn_selct_to_fb);
			}
			return view;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}
		
	}
	
	public void addList(ArrayList<String> list){
		this.list.clear();
		this.list = list;
		adapter.notifyDataSetChanged();
	}
	
	public void onDismissGetValue(getSelectText text){
		this.getSelectText = text;
	}

	public interface getSelectText {
		void getValue(String card);
	}
	public interface CloseView {
		void getView();
	}

}
